package com.moogene.android.engine.mmi;

import android.graphics.Canvas;
import android.util.Log;

import com.moogene.android.InfoMediator;

public class Camera extends Layer{

	private static final String TAG = "Camera";
	
	private static final int FOLLOW_GAP = 100;
	
	private int xBoarder;
	
	private int yBoarder;

	public Camera() {
		super(InfoMediator.getScreenWidth(), InfoMediator.getScreenHeight());
		setLocation(0,0);
	}

	@Override
	public void draw(Canvas canvas) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update() {
		log("x: " + x + "; y:" + y);
		
	}
	
    @Override
    protected boolean canDragFree() {
        return true;
    }
    
    void setBoarder(int xBoarder, int yBoarder){
    	this.xBoarder = xBoarder;
    	this.yBoarder = yBoarder;
    }

	boolean checkBoarder() {
		boolean hasBeenAdjust = false;
		if( x < 0){
			x = 0;
			hasBeenAdjust = true;
		}
		if( x + InfoMediator.getScreenWidth() > xBoarder){
			x = xBoarder - InfoMediator.getScreenWidth();
			hasBeenAdjust = true;
		}
		if( y < 0){
			y = 0;
			hasBeenAdjust = true;
		}
		if( y + InfoMediator.getScreenHeight() > yBoarder){
			y = yBoarder - InfoMediator.getScreenHeight();
			hasBeenAdjust = true;
		}
		return hasBeenAdjust;
	}
	
    static void log(String msg) {
        if (Config.LOG_ENABLE) {
            Log.d(Config.LOG_TAG + "-"
                    + TAG, msg);
        }
    }

	void follow(int screenX, int screenY) {
		if(screenX < FOLLOW_GAP){
			x -= FOLLOW_GAP - screenX;
		}else if(screenX > InfoMediator.getScreenWidth() - FOLLOW_GAP){
			x += screenX - InfoMediator.getScreenWidth() + FOLLOW_GAP;
		}
		if(screenY < FOLLOW_GAP){
			y -= FOLLOW_GAP - screenY;
		}else if(screenY > InfoMediator.getScreenHeight() - FOLLOW_GAP){
			y += screenY - InfoMediator.getScreenHeight() + FOLLOW_GAP;
		}
		checkBoarder();
	}

}
